package com.viknix.threadpool.manager.client.exception;

import lombok.Data;

/**
 * @Author: Dongqi
 * @Date: 2021/11/3 14:31
 * @Version 1.0
 * @Description: 客户端异常类
 */
@Data
public class TpmProcessException extends RuntimeException {

    private int errCode;
    private String errMsg;
    private Throwable causeThrowable;
    public static final int CLIENT_INVALID_PARAM = -400;
    public static final int CLIENT_OVER_THRESHOLD = -503;
    public static final int INVALID_PARAM = 400;
    public static final int NO_RIGHT = 403;
    public static final int NOT_FOUND = 404;
    public static final int CONFLICT = 409;
    public static final int SERVER_ERROR = 500;
    public static final int BAD_GATEWAY = 502;
    public static final int OVER_THRESHOLD = 503;

    public TpmProcessException(String errMsg) {
        super(errMsg);
    }

    public TpmProcessException(int errCode, String errMsg) {
        super(errMsg);
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public TpmProcessException(int errCode, Throwable throwable) {
        super(throwable);
        this.errCode = errCode;
        this.setCauseThrowable(throwable);
    }

    public TpmProcessException(int errCode, String errMsg, Throwable throwable) {
        super(errMsg, throwable);
        this.errCode = errCode;
        this.errMsg = errMsg;
        this.setCauseThrowable(throwable);
    }

    private Throwable getCauseThrowable(Throwable t) {
        return t.getCause() == null ? t : this.getCauseThrowable(t.getCause());
    }

    @Override
    public String toString() {
        return "ErrCode:" + this.getErrCode() + ", ErrMsg:" + this.getErrMsg();
    }
}
